package 线程状态.线程优先级;

/**
 * 线程优先级是从小到大范围是{1-10}，数字越大优先级越高，（但实际运行还是看cpu的调度）
 * 线程默认优先级是5,(对主线程的优先级进行修改后，在主线程下启动的线程优先级一会随之改变“变成与主线程优先级一致”)
 * 设置线程优先级超过范围会报错
 * 设置线程优先级一定要在启动线程之前设置，否则无效。
 */
public class test1 {
    public static void main(String[] args) {
        //主线程的优先级
        System.out.println(Thread.currentThread().getName()+"-->"+Thread.currentThread().getPriority());
        //通过引用修改主线程优先级
        Thread.currentThread().setPriority(1);
        System.out.println(Thread.currentThread().getName()+"-->"+Thread.currentThread().getPriority());

        //其他创建的线程
        yx yx = new yx();
        Thread t1 = new Thread(yx,"t1");
        Thread t2 = new Thread(yx,"t2");
        Thread t3 = new Thread(yx,"t3");
        Thread t4 = new Thread(yx,"t4");
        Thread t5 = new Thread(yx,"t5");
        Thread t6 = new Thread(yx,"t6");
        Thread t7 = new Thread(yx,"t7");
        Thread t8 = new Thread(yx,"t8");



        t1.start();
        //获取默认的线程优先级
        System.out.println(t2.getName()+"默认优先级---->"+t2.getPriority());
        t2.setPriority(3);
        //获取修改的线程优先级
        System.out.println(t2.getName()+"修改后的优先级---->"+t2.getPriority());
        t2.start();

        t3.setPriority(5);
        t3.start();
        t4.start();
        t5.start();
        t6.start();
        t7.start();

        t8.setPriority(9);
        t8.start();



    }
}
class yx implements Runnable{

    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName()+"-->"+Thread.currentThread().getPriority());//Thread.currentThread().getPriority()获取线程优先级
    }
}
